
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# 可以对网页上元素是否存在，可点击等等进行判断，一般用于断言或与WebDriverWait配合使用。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

brower = webdriver.Chrome(executable_path="chromedriver.exe")
url = "http://47.103.13.124:8001/webdriver"
# url = "http://47.103.13.124:8001/iframe_out"
# brower.get(url)
# brower.add_cookie({'name':'session','value':'.eJyrViotTi1SsqpWyiyOT0zJzcxTsjLQUcrJTwexSopKU3WUcvOTMnNSlayUDM3gQEkHrDE-M0XJyhjCzkvMBSmKKTVNMjMDkiamFkq1tQDfeR3n.YMSFxw.zqmqHWReTQy-hZrWiGfXnUzvbZA'})
# brower.get(url)
def wait_element(brower,element_id,wait_time=10):
    try:
        # 调用该方法提供的驱动程序作为参数，直到返回值为True
        WebDriverWait(brower,wait_element,1).until(
            # 判断是否至少有1个元素存在于dom树中。
            # 举个例子，如果页面上有n个元素的class都是'column-md-3'，那么只要有1个元素存在，这个方法就返回True
            EC.presence_of_all_elements_located((By.ID,element_id))
        )
    except Exception as e:
        raise Exception(e)

try:
    brower.get(url)
    brower.add_cookie({'name':'session','value':'.eJyrViotTi1SsqpWyiyOT0zJzcxTsjLQUcrJTwexSopKU3WUcvOTMnNSlayUDM3gQEkHrDE-M0XJyhjCzkvMBSmKKTVNMjMDkiamFkq1tQDfeR3n.YMSFxw.zqmqHWReTQy-hZrWiGfXnUzvbZA'})
    brower.get(url)
    myframe = brower.find_element_by_id('myiframe')
    brower.switch_to.frame(myframe)
    input = 'inputEmail'
    # wait_element(brower,input)
    user = brower.find_element_by_id(input)
    user.clear()
    # user.click()
    user.send_keys('user')

    # time.sleep(2)
    p = 'inputPassword'
    # wait_element(brower,p)
    padd = brower.find_element_by_id(p)
    # padd.click()
    padd.clear()
    padd.send_keys('password')

    # brower.switch_to.parent_frame(myframe)
    button = brower.find_element_by_tag_name('div').find_element_by_tag_name('button')
    button.click()
    print('已经点击')
finally:
    time.sleep(10)
    brower.close()








































